package org.rcgwt.client.ui.calendar.renderer.impl;

import org.rcgwt.client.ui.calendar.controller.CalendarAction;
import org.rcgwt.client.ui.calendar.controller.CalendarChangesMask;
import org.rcgwt.client.ui.calendar.controller.CalendarController;
import org.rcgwt.client.ui.calendar.event.ModelChangedEvent;
import org.rcgwt.client.ui.calendar.event.ModelChangedHandler;
import org.rcgwt.client.ui.calendar.i18n.ConstantsProvider;
import org.rcgwt.client.ui.calendar.model.impl.CalendarModel;
import org.rcgwt.client.ui.calendar.model.impl.SelectionType;
import org.rcgwt.client.ui.calendar.mvc.ICalendarContext;
import org.rcgwt.client.ui.calendar.renderer.IFooterRenderer;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyDownEvent;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.MouseWheelEvent;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;

/**
 * The calendar footer renderer
 * 
 * @author Elian
 * 
 */

public class CalendarFooterRenderer implements IFooterRenderer {

	private FlexTable semanticNavigationPanel;

	/* Today Button */
	private Label b_today;

	/* Semantic Navigation Controls */
	private Label b_next;
	private Label b_previous;

	private Label selectionTypeLabel;

	private CalendarController controller;
	private CalendarModel model;

	private static ConstantsProvider constants = GWT
			.create(ConstantsProvider.class);

	public CalendarFooterRenderer() {

	}

	/**
	 * Rendering method
	 */

	@Override
	public Widget render(ICalendarContext context) {

		controller = context.getController();
		model = context.getModel();

		semanticNavigationPanel = new FlexTable();
		semanticNavigationPanel.setStyleName(context.getTheme()
				.getSemanticNavigationPanelStyle().getClassName());

		selectionTypeLabel = new Label();
		setSelectionTypeLabelText(SelectionType.NONE);

		b_today = new Label(constants.today());
		b_today.setStyleName(context.getTheme().getTodayButtonStyle()
				.getClassName());

		b_previous = new Label();
		b_previous.addStyleName(context.getTheme().getPreviousButtonStyle()
				.getClassName());

		b_next = new Label();
		b_next.addStyleName(context.getTheme().getNextButtonStyle()
				.getClassName());

		semanticNavigationPanel.setWidget(0, 0, b_previous);
		semanticNavigationPanel.setWidget(0, 1, selectionTypeLabel);
		semanticNavigationPanel.setWidget(0, 2, b_next);
		semanticNavigationPanel.setWidget(1, 1, b_today);

		/* Handlers Management */

		b_previous.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				controller.handle(CalendarAction.PREVIOUS_CONTEXT);
			}
		});
		b_next.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				controller.handle(CalendarAction.NEXT_CONTEXT);
			}
		});
		b_today.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				controller.handle(CalendarAction.DISPLAY_TODAY);
			}
		});

		createHandlers();
		return semanticNavigationPanel;
	}

	/**
	 * Create and manages handlers
	 */

	private void createHandlers() {
		controller.addModelChangedHandler(new ModelChangedHandler() {
			@Override
			public void onModelChangedEvent(ModelChangedEvent event) {
				/* To set the semantic context label value */
				if ((event.getElementType() & CalendarChangesMask.SELECTION_TYPE) > 0) {
					setSelectionTypeLabelText(model.getSelectionType());
				}
			}
		});
	}

	/**
	 * Sets the semantic navigation context label value
	 * 
	 * @param type
	 */

	private void setSelectionTypeLabelText(SelectionType type) {
		String text = "";
		switch (type) {
		case NONE:
			text = constants.noneSelection();
			break;
		case YEAR:
			text = constants.yearSelection();
			break;
		case MONTH:
			text = constants.monthSelection();
			break;
		case DAYS:
			text = constants.daysSelection();
			break;
		case WEEK:
			text = constants.weekSelection();
			break;
		case DATE:
			text = constants.dateSelection();
			break;
		}
		this.selectionTypeLabel.setText(text);
	}

	@Override
	public void update(ICalendarContext context) {
	}

	@Override
	public void onKeyDown(KeyDownEvent event) {
	}

	@Override
	public void onKeyUp(KeyUpEvent event) {
	}

	@Override
	public void onMouseWheel(MouseWheelEvent event) {
	}
}
